<template>
  <div class="layout">
    <header class="header">
      <div class="container">
        <h1 class="logo">
          <router-link to="/">Blog System</router-link>
        </h1>
        <nav class="nav">
          <router-link to="/">首页</router-link>
          <router-link to="/category/1">技术</router-link>
          <router-link to="/category/2">生活</router-link>
          <div class="user-menu">
            <template v-if="isLoggedIn">
              <el-dropdown>
                <span class="user-info">
                  {{ user?.nickname || user?.username }}
                  <el-icon><arrow-down /></el-icon>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item @click="$router.push('/admin')">
                      管理后台
                    </el-dropdown-item>
                    <el-dropdown-item @click="logout">
                      退出登录
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
            <template v-else>
              <router-link to="/login" class="login-btn">登录</router-link>
            </template>
          </div>
        </nav>
      </div>
    </header>
    
    <main class="main">
      <div class="container">
        <router-view />
      </div>
    </main>
    
    <footer class="footer">
      <div class="container">
        <p>&copy; 2024 Blog System. All rights reserved.</p>
      </div>
    </footer>
  </div>
</template>

<script setup lang="ts">
import { useUserStore } from '@/store/user'

const userStore = useUserStore()
const { user, isLoggedIn, logout } = userStore

onMounted(() => {
  userStore.initAuth()
})
</script>

<style scoped>
.layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: #fff;
  border-bottom: 1px solid #eee;
  padding: 0 20px;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
}

.logo a {
  color: #409eff;
  text-decoration: none;
  font-weight: bold;
}

.nav {
  display: flex;
  align-items: center;
  gap: 30px;
}

.nav a {
  color: #666;
  text-decoration: none;
  transition: color 0.3s;
}

.nav a:hover,
.nav a.router-link-active {
  color: #409eff;
}

.user-menu {
  margin-left: 20px;
}

.user-info {
  cursor: pointer;
  color: #666;
}

.login-btn {
  padding: 8px 16px;
  background: #409eff;
  color: white !important;
  border-radius: 4px;
  text-decoration: none;
}

.main {
  flex: 1;
  padding: 20px;
}

.footer {
  background: #f5f5f5;
  text-align: center;
  padding: 20px;
  color: #666;
}
</style>